import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';

typedef GestureTapCallback = void Function();

class ContentView extends StatelessWidget {
  // 标题
  final String title;

  //是否必填的标志
  final String mustType;

  //输入框 显示的灰色提示文字
  final String hintText;

  final TextEditingController controller;

  //下拉框数组
  final List contentList;

  // 输入框是否只读 默认 false
  final bool readOnly;

  //文字显示的位置
  final TextAlign textAlign;

  //输入框键盘样式
  final TextInputType keyboardType;

  //输入框右边👉显示的widget
  final Widget suffixIcon;

  //判断 类型 是 输入 还是 下拉选择
  final bool style;

  // 下拉框点击事件onSubmited
  final ValueChanged<String> onChanged;

  // 下拉框点击事件onSubmited
  final ValueChanged<String> onSubmited;

  // 输入框输入的类型限制，只能输入数字、汉字等
  final List<TextInputFormatter> inputFormatters;

  // 输入框不可输入时的点击事件
  final GestureTapCallback onTap;
  //光标焦点
  final FocusNode focusNode;

  //输入框左边title的宽度
  final num titleWidth;

  //标题颜色
  Color titleColor; //标题颜色

  //下方 横线 距离 左右 的 距离 默认0
  final double dividerSpace;

  //横线高度
  final double dividerHeight;

  // 是否显示 右边 箭头
  final bool isShowRightArrow;

  //输入的文字 颜色
  final Color valueColor;

  ContentView({
    this.title = '',
    this.mustType = '',
    this.hintText = '',
    this.controller,
    this.contentList,
    this.readOnly = false,
    this.textAlign,
    this.keyboardType,
    this.suffixIcon,
    this.style = false,
    this.onChanged,
    this.onSubmited,
    this.inputFormatters,
    this.onTap,
    this.focusNode,
    this.titleWidth,
    this.titleColor,
    this.dividerSpace = 0,
    this.dividerHeight = 1,
    this.isShowRightArrow = false,
    this.valueColor
  });

  factory ContentView.textfield({
    String title = '',
    Color titleColor,
    String mustType = '',
    String hintText = '',
    @required TextEditingController controller,
    bool readOnly = false,
    TextAlign textAlign = TextAlign.start,
    TextInputType keyboardType,
    Widget suffixIcon,
    ValueChanged<String> onChanged,
    ValueChanged<String> onSubmited,
    List<TextInputFormatter> inputFormatters,
    GestureTapCallback onTap,
    FocusNode focusNode,
    num titleWidth,
    double dividerSpace,
    double dividerHeight,
    bool isShowRightArrow = false,
    Color valueColor,
  }) {
    return ContentView(
      controller: controller,
      title: title,
      mustType: mustType,
      hintText: hintText,
      readOnly: readOnly,
      textAlign: textAlign,
      keyboardType: keyboardType,
      suffixIcon: suffixIcon,
      style: false,
      onChanged: onChanged,
      onSubmited: onSubmited,
      inputFormatters: inputFormatters,
      onTap: onTap,
      focusNode: focusNode,
      titleWidth: titleWidth,
      titleColor: titleColor,
      dividerSpace: dividerSpace,
      dividerHeight: dividerHeight,
      isShowRightArrow: isShowRightArrow,
      valueColor: valueColor,
    );
  }

  factory ContentView.dropdownView({
    String title = '',
    Color titleColor,
    String mustType = '',
    String hintText = '',
    @required TextEditingController controller,
    @required List contentList,
    TextAlign textAlign = TextAlign.start,
    @required ValueChanged<String> onChanged,
    @required ValueChanged<String> onSubmited,
    num titleWidth,
    double dividerSpace,
    double dividerHeight,
  }) {
    return ContentView(
      controller: controller,
      title: title,
      titleColor: titleColor,
      mustType: mustType,
      hintText: hintText,
      contentList: contentList,
      textAlign: textAlign,
      style: true,
      onChanged: onChanged,
      onSubmited: onSubmited,
      titleWidth: titleWidth,
      dividerSpace: dividerSpace,
      dividerHeight: dividerHeight,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Container(
          padding: EdgeInsets.fromLTRB(24, 0, 0, 0),
          color: Colors.white,
          child: Row(
            children: [
              Row(
                children: <Widget>[
                  Text(
                    mustType,
                    style: TextStyle(color: Colors.red),
                  ),
                  Text(
                    title,
                    style:
                    TextStyle(fontSize: Dimens.textSize28, color: titleColor),
                  ),
                ],
              ),
              Expanded(child: Container(),flex: 1,),
              Container(

                padding: EdgeInsets.fromLTRB(0, 0, 20, 0),
                child: this.style == false
                    ? Row(
                  children: [
                    Expanded(
                      child: TextField(
                        keyboardType: this.keyboardType,

                        readOnly: this.readOnly,
                        controller: controller,
                        textAlign: this.textAlign,
                        onChanged: this.onChanged,
                        onSubmitted: this.onSubmited,
                        inputFormatters: this.inputFormatters,
                        onTap: this.onTap,
                        maxLines: 5,
                        minLines: 1,
                        style: TextStyle(fontSize: Dimens.textSize28),
                        focusNode: focusNode,
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: hintText,
                          hintStyle: TextStyle(fontSize: Dimens.textSize28),
                          suffixIcon: this.suffixIcon,
                        ),
                      ),
                    ),
                    Offstage(
                        offstage: !this.isShowRightArrow,
                        child: Icon(
                          Icons.arrow_forward_ios_rounded,
                          size: 17,
                        ))
                  ],
                )
                    : Container(
                      height: 45,
                      child: DropdownButtonFormField(
                  hint: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(hintText, style: TextStyle(fontSize: Dimens.textSize28))),

                    decoration: InputDecoration(
                      border: InputBorder.none,
                    ),
                    isExpanded: true,
                    value: controller.text == '' ? null : controller.text,
                    items: contentList
                        .map((title) => DropdownMenuItem(
                      child: Container(
                        alignment: Alignment.centerLeft,
                        child: Text(title,style: TextStyle(fontSize: Dimens.textSize28),),
                      ),
                      value: title,
                    ))
                        .toList(),
                    onChanged: (title) {
                      controller.text = title;
                      this.onChanged(title);
                    }),
                    ),
                width: MediaQuery.of(context).size.width -
                    (this.titleWidth == null ? 136 : this.titleWidth),
              )
            ],
          )



          // ListTile(
          //   title:
          //   trailing:,
          //
          // ),
        ),
        Offstage(
          offstage: dividerHeight == 0 ? true : false,
          child:Container(
            margin: EdgeInsets.only(left: 16,right: 16),
            child:  Divider(

              height: 1,
              indent: dividerSpace,
              endIndent: dividerSpace,
            ),
          ),
        ),
      ],
    );
  }
}
